package com.lqa.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqa.mapper.EbProductMapper;
import com.lqa.model.EbProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service  //----service层
public class EbProductService {
    @Autowired
    private EbProductMapper ebProductMapper;

    /**
     * @param psize
     * @param limit
     * @return
     */
    public Page<EbProduct> list(int psize,int limit){//导包 alt+enter

        Page<EbProduct> page =new Page(psize,limit);
        //page.set
        return ebProductMapper.selectPage(page,null);


    }
    public EbProduct detail(int epId){

        return ebProductMapper.selectById(epId);
    }
    public void addCar(int epId, HttpServletRequest request){
        //1.TODO 调用mapper获取商品对象
        EbProduct newProduct =ebProductMapper.selectById(epId);
        //2.TODO 创造购物车（session）
        HttpSession session=request.getSession();
        //3.TODO 得到集合？（1.创建 2.从session获取）
        List<EbProduct> list=null;
        //3.1 先从购物车中获取种子
        list=(List<EbProduct>)session.getAttribute("car");
        if(list==null) {
            list = new ArrayList<>();
        }
        //4.TODO 将商品放入集合
        list.add(newProduct);
        //5.TODO 将集合放入购物车中
        session.setAttribute("car",list);
    }

}